package com.example.demo.service;

import com.example.demo.entity.Inventory;
import com.example.demo.repository.InventoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 库存服务类
 */
@Service
@Transactional
public class InventoryService {
    
    @Autowired
    private InventoryRepository inventoryRepository;
    
    public Inventory save(Inventory inventory) {
        return inventoryRepository.save(inventory);
    }
    
    public Optional<Inventory> findById(Long id) {
        return inventoryRepository.findById(id);
    }
    
    public Optional<Inventory> findByProductIdAndWarehouseId(Long productId, Long warehouseId) {
        return inventoryRepository.findByProductIdAndWarehouseId(productId, warehouseId);
    }
    
    public List<Inventory> findByProductId(Long productId) {
        return inventoryRepository.findByProductId(productId);
    }
    
    public List<Inventory> findByWarehouseId(Long warehouseId) {
        return inventoryRepository.findByWarehouseId(warehouseId);
    }
    
    public List<Inventory> findByStatus(Inventory.InventoryStatus status) {
        return inventoryRepository.findByStatus(status);
    }
    
    public List<Inventory> findLowStockItems() {
        return inventoryRepository.findLowStockItems();
    }
    
    public List<Inventory> findOutOfStockItems() {
        return inventoryRepository.findOutOfStockItems();
    }
    
    public List<Inventory> findOverstockItems() {
        return inventoryRepository.findOverstockItems();
    }
    
    /**
     * 入库操作
     */
    public Inventory inbound(Long productId, Long warehouseId, Integer quantity) {
        Optional<Inventory> inventoryOpt = findByProductIdAndWarehouseId(productId, warehouseId);
        Inventory inventory;
        
        if (inventoryOpt.isPresent()) {
            inventory = inventoryOpt.get();
            inventory.setCurrentStock(inventory.getCurrentStock() + quantity);
            inventory.setAvailableStock(inventory.getAvailableStock() + quantity);
        } else {
            inventory = new Inventory();
            inventory.setProductId(productId);
            inventory.setWarehouseId(warehouseId);
            inventory.setCurrentStock(quantity);
            inventory.setAvailableStock(quantity);
            inventory.setReservedStock(0);
        }
        
        updateInventoryStatus(inventory);
        return inventoryRepository.save(inventory);
    }
    
    /**
     * 出库操作
     */
    public Inventory outbound(Long productId, Long warehouseId, Integer quantity) {
        Optional<Inventory> inventoryOpt = findByProductIdAndWarehouseId(productId, warehouseId);
        if (!inventoryOpt.isPresent()) {
            throw new RuntimeException("库存不存在");
        }
        
        Inventory inventory = inventoryOpt.get();
        if (inventory.getAvailableStock() < quantity) {
            throw new RuntimeException("库存不足");
        }
        
        inventory.setCurrentStock(inventory.getCurrentStock() - quantity);
        inventory.setAvailableStock(inventory.getAvailableStock() - quantity);
        
        updateInventoryStatus(inventory);
        return inventoryRepository.save(inventory);
    }
    
    /**
     * 预留库存
     */
    public Inventory reserve(Long productId, Long warehouseId, Integer quantity) {
        Optional<Inventory> inventoryOpt = findByProductIdAndWarehouseId(productId, warehouseId);
        if (!inventoryOpt.isPresent()) {
            throw new RuntimeException("库存不存在");
        }
        
        Inventory inventory = inventoryOpt.get();
        if (inventory.getAvailableStock() < quantity) {
            throw new RuntimeException("可用库存不足");
        }
        
        inventory.setReservedStock(inventory.getReservedStock() + quantity);
        inventory.setAvailableStock(inventory.getAvailableStock() - quantity);
        
        return inventoryRepository.save(inventory);
    }
    
    /**
     * 释放预留库存
     */
    public Inventory release(Long productId, Long warehouseId, Integer quantity) {
        Optional<Inventory> inventoryOpt = findByProductIdAndWarehouseId(productId, warehouseId);
        if (!inventoryOpt.isPresent()) {
            throw new RuntimeException("库存不存在");
        }
        
        Inventory inventory = inventoryOpt.get();
        inventory.setReservedStock(inventory.getReservedStock() - quantity);
        inventory.setAvailableStock(inventory.getAvailableStock() + quantity);
        
        updateInventoryStatus(inventory);
        return inventoryRepository.save(inventory);
    }
    
    /**
     * 更新库存状态
     */
    private void updateInventoryStatus(Inventory inventory) {
        if (inventory.getCurrentStock() == 0) {
            inventory.setStatus(Inventory.InventoryStatus.OUT_OF_STOCK);
        } else if (inventory.getCurrentStock() <= inventory.getMinStockLevel()) {
            inventory.setStatus(Inventory.InventoryStatus.LOW_STOCK);
        } else if (inventory.getMaxStockLevel() != null && inventory.getCurrentStock() > inventory.getMaxStockLevel()) {
            inventory.setStatus(Inventory.InventoryStatus.OVERSTOCK);
        } else {
            inventory.setStatus(Inventory.InventoryStatus.NORMAL);
        }
    }
    
    public void deleteById(Long id) {
        inventoryRepository.deleteById(id);
    }
    
    public Inventory update(Inventory inventory) {
        updateInventoryStatus(inventory);
        return inventoryRepository.save(inventory);
    }
}
